<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2018/11/23
 * Time: 10:25 AM
 */

namespace app\shop\controller;

use app\shop\model\Goods as GoodsModel;
use app\shop\model\GoodsAccessory;
use app\shop\model\GoodsClientPrice;
use app\shop\model\Goodsdescribe;

use app\shop\model\GoodsLevel;
use app\shop\model\GoodsLevelPrice;
use app\shop\model\GoodsUnits;
use app\shop\model\Unit;
use app\shop\model\Accessory as accessoryModel;
use app\shop\model\GoodsSku as GoodsSkuModel;
use app\shop\model\Img;
use think\Db;
use think\Exception;
use think\Loader;
use think\Request;
use app\shop\model\GoodsImg as GoodsImgModel;

class Goods extends \ShopController
{

    /**
     * 添加商品
     * @param Request $request
     */
    public function save(Request $request)
    {

        $params = $request->param();
        //var_dump($params);die;
        if(!empty($params['goods_accessory'])){
            $goods_accessory = $params['goods_accessory'];
            unset($params['goods_accessory']);
        }
        if(!empty($params['Goods_imgs'])){
            $goods_imgs = $params['Goods_imgs'];
            unset($params['Goods_imgs']);
        }
        $sn = new \app\shop\model\Sn();
        $im = $sn->getSn('im',1);
        try {

            $GoodsModel = new GoodsModel();
            $GoodsModel->startTrans();
            $GoodsDb = $GoodsModel->GoodsDb();

            //新增商品
            //············商品规格名
            $specification_name = $params['specification_name'];
            unset($params['specification_name']);
            $addGoods['specification1_name'] = isset($specification_name[0]) ? $specification_name[0]['specName'] : '';
            $addGoods['specification2_name'] = isset($specification_name[1]) ? $specification_name[1]['specName'] : '';
            $addGoods['specification3_name'] = isset($specification_name[2]) ? $specification_name[2]['specName'] : '';
            $addGoods['goods_name'] = $params['goods_name'];
            $addGoods['cate_id'] = $params['cate_id'];
            $addGoods['is_units'] = $this->boolFormatInt($params['is_units']);
            $addGoods['is_client_price'] = $this->boolFormatInt($params['is_client_price']);
            $addGoods['sort_num'] = $params['sort_num'];
            $addGoods['keywords'] = !empty($params['keywords']) ? implode(',',$params['keywords']) : '';
            $addGoods['specification_id'] = intval($im[0]);
            $addGoods['is_new'] = $this->boolFormatInt($params['is_new']);
            $addGoods['is_hot'] = $this->boolFormatInt($params['is_hot']);
            $addGoods['goods_state'] = $this->boolFormatInt($params['goods_state']);
            $addGoods['cost_price'] = $params['cost_price'];
            $addGoods['add_goods_time'] = date('Y-m-d H:i:s');
            $addGoods['update_goods_time'] = date('Y-m-d H:i:s');
            $addGoodsIds = [];
            $specificatio = $params['specificatio'];

            foreach ($specificatio as $gKey=>$gValue){
                $addGoods['goods_sn'] = $gValue['goods_sn'];
                $addGoods['goods_code1'] = $gValue['goods_code1'];
                $addGoods['goods_code2'] = $gValue['goods_code2'];
                $addGoods['goods_code3'] = $gValue['goods_code3'];
                $addGoods['goods_img'] = $gValue['goods_img'];
                $addGoods['specification1_value'] = isset($gValue['specValues'][0]) ? $gValue['specValues'][0] : '';
                $addGoods['specification2_value'] = isset($gValue['specValues'][1]) ? $gValue['specValues'][1] : '';
                $addGoods['specification3_value'] = isset($gValue['specValues'][2]) ? $gValue['specValues'][2] : '';
                $addGoodsIds[] = $GoodsDb->insertGetId($addGoods);

            }

            $imgModel = new Img();
            $imgDb = $imgModel->ImgDb();
            $accessoryModel = new accessoryModel();
            $AccessoryDb = $accessoryModel->AccessoryDb();
            //新增商品描述

            $GoodsdescribeModel = new Goodsdescribe();
            $GoodsdescribeDb = $GoodsdescribeModel->GoodsdescribeDb();
            $addDescribe['describe_content'] =  $params['goods_describe'];
            foreach ($addGoodsIds as $Idsv){
                $addDescribe['goods_id'] = $Idsv;
                $GoodsdescribeDb->insert($addDescribe);
            }

            //新增图片

            if(!empty($goods_imgs)){
                $GoodsimgModel = new GoodsImgModel();
                $GoodsimgDb = $GoodsimgModel->GoodsimgDb();
                foreach ($goods_imgs as $v){
                    $iid = $imgDb->insertGetId(['img_url'=>$v]);
                    $addGoodsimg['img_id'] = $iid;
                    foreach ($addGoodsIds as $Idsv1){
                        $addGoodsimg['goods_id'] = $Idsv1;
                        $GoodsimgDb->insert($addGoodsimg);
                    }
                }
            }
            
            //新增附件
            if(!empty($goods_accessory)){
                $GoodsAccessoryModel = new GoodsAccessory();
                $GoodsAccessoryDb = $GoodsAccessoryModel->GoodsAccessoryDb();
    
                foreach ($goods_accessory as $v1){
                    $arrAccessory = explode('|',$v1);
                    $aid = $AccessoryDb->insertGetId(['accessory_url'=>$arrAccessory[0],'accessory_name'=>$arrAccessory[1]]);
                    $addGoodsAccessory['accessory_id'] = $aid;
                    foreach ($addGoodsIds as $Idsv2){
                        $addGoodsAccessory['goods_id'] = $Idsv2;
                        $GoodsAccessoryDb->insert($addGoodsAccessory);
                    }
    
                }
            }
            
            //保存商品多单位
            $addUnit['initial_unit_id'] = $params['initial_unit_id'];
            $addUnit['is_use_initial_unit'] = $this->boolFormatInt($params['is_use_initial_unit']);
            $addUnit['initial_unit_price'] = $params['initial_unit_price'];
            $addUnit['extra1_unit_id'] = $params['extra1_unit_id'];
            $addUnit['extra1_unit_price'] = $params['extra1_unit_price'];
            $addUnit['extra1_unit_ratio'] = $params['extra1_unit_ratio'];
            $addUnit['is_use_extra1_unit'] = $this->boolFormatInt($params['is_use_extra1_unit']);
            $addUnit['extra2_unit_id'] = $params['extra2_unit_id'];
            $addUnit['extra2_unit_price'] = $params['extra2_unit_price'];
            $addUnit['extra2_unit_ratio'] = $params['extra2_unit_ratio'];
            $addUnit['is_use_extra2_unit'] = $this->boolFormatInt($params['is_use_extra2_unit']);
            $GoodsUnitsModel =  new GoodsUnits();
            $GoodsUnitsDb = $GoodsUnitsModel->GoodsUnitsDb();
            foreach ($addGoodsIds as $Ids3){
                $addUnit['goods_id'] = $Ids3;
                $GoodsUnitsDb->insert($addUnit);
            }

            //保存商品客户价
            $client_price = $params['client_price'];
            $GoodsClientPrice = new GoodsClientPrice();
            $GoodsClientPriceDb = $GoodsClientPrice->GoodsClientPriceDb();
            foreach ($client_price as $ClientPriceK =>$ClientPriceV){
                $addClientPrice['client_id'] = $ClientPriceV['client_id'];
                $addClientPrice['client_purchase'] = $this->boolFormatInt($ClientPriceV['client_purchase']);
                $addClientPrice['client_price1'] = $ClientPriceV['client_price1'];
                $addClientPrice['client_price2'] = $ClientPriceV['client_price2'];
                $addClientPrice['client_price3'] = $ClientPriceV['client_price3'];
                $addClientPrice['client_num'] = $ClientPriceV['client_num'];
                foreach ($addGoodsIds as $Ids4){
                    $addClientPrice['goods_id'] = $Ids4;
                    $GoodsClientPriceDb->insert($addClientPrice);
                }

            }

            //保存商品等级价
            $level_price = $params['level_price'];
            $GoodsLevelPrice = new GoodsLevelPrice();
            $GoodsLevelPriceDb = $GoodsLevelPrice->GoodsLevelPriceDb();
            foreach ($level_price as $levelPriceK =>$levelPriceV){
                $addLevelPrice['level_id'] = $levelPriceV['level_id'];
                $addLevelPrice['discount'] = $levelPriceV['discount'];
                $addLevelPrice['level_purchase'] = $this->boolFormatInt($levelPriceV['level_purchase']);
                $addLevelPrice['level_price1'] = ($levelPriceV['discount'] / 100) * $params['initial_unit_price'];
                $addLevelPrice['level_price2'] = !empty($params['extra1_unit_id']) ? ($levelPriceV['discount'] / 100) * $params['extra1_unit_price']  : 0;
                $addLevelPrice['level_price3'] =!empty($params['extra2_unit_id']) ? ($levelPriceV['discount'] / 100) * $params['extra2_unit_price'] : 0;


                $addLevelPrice['level_num'] = $levelPriceV['level_num'];
                foreach ($addGoodsIds as $Ids5){
                    $addLevelPrice['goods_id'] = $Ids5;
                    $GoodsLevelPriceDb->insert($addLevelPrice);
                }

            }
            $GoodsModel->commit();
        } catch (Exception $e) {

            $GoodsModel->rollback();
            $this->serverErrorTerminate($e->getMessage());
        }
        $this->successTerminate();
    }
    /**
     * 获取商品列表
     * @OA\Get(tags={"Goods"}, path="/shop/goods/page", summary="商品分页列表",
     *   @OA\Parameter(name="currentPage", description="当前页 默认1", in="query", @OA\Schema(type="integer"), ),
     *   @OA\Parameter(name="pageSize", description="每页显示商品数量 默认20", in="query", @OA\Schema(type="integer"), ),
     *   @OA\Response(response="200", description="请求成功，返回商品列表",
     *     @OA\JsonContent(type="array", @OA\Items(ref="#/components/schemas/Goods")),
     *   ),
     * )
     */
    public function index(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->search($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    //
    public function clientGoodsInfo(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->search2($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    public function clientGoodsList(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->search3($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }

    }
    
    public function page()
    {
        $currentPage = input('get.currentPage', 1, 'intval');
        $pageSize = input('get.pageSize', 20, 'intval');

        $_pageFirst = ($currentPage - 1) * $pageSize;
        $_totalNum = $this->_total();

        $goodsIds = GoodsSkuModel::where([
            'is_delete' => 0,
        ])->order('sort_order', 'desc')
            ->limit($_pageFirst, $pageSize)
            ->column('goods_id');

        $this->successTerminate([
            'totalNum' => $_totalNum,
            'list' => $this->goodsList($goodsIds),
        ]);
    }
    /**
     * 获取商品列表
     */

    public function updateinfo(Request $request){
        $id= $request->get('goods_id');

        $goodsModel = new \app\shop\model\Goods();
        $GoodsLevelPrice = new GoodsLevelPrice();
        $GoodsClientPrice = new GoodsClientPrice();
        
        $GoodsImg = new GoodsImgModel();
        $GoodsimgDb = $GoodsImg->GoodsimgDb();
        $GoodsAccessory = new  GoodsAccessory();
        
        try {
            $data = $goodsModel->alias('g')
            ->where('g.goods_id',$id)
            ->join('sh_goods_units u','u.goods_id = g.goods_id')
            ->join('sh_goods_describe d','d.goods_id = g.goods_id')
            ->field('g.*,u.*,d.describe_content as goods_describe')
            ->find()->toArray();
            $data['produced_time'] = date('Y-m-d',strtotime($data['produced_time']));
            $data['level_price'] = $GoodsLevelPrice->alias('a')
            ->join('sh_client_level b','a.level_id = b.level_id')
            ->field('a.*,b.level_name')
            ->where('goods_id',$id)
            ->select();
            $data['client_price'] = $GoodsClientPrice->alias('a')
            ->join('sh_client b','a.client_id = b.client_id')
            ->join('sh_client_level c','b.level_id = c.level_id')
            ->field('a.*,c.level_name')
            ->where('goods_id',$id)
            ->select();
            $data['goods_accessory'] = $GoodsAccessory->alias('b')
            ->join('sh_accessory c','b.accessory_id=c.accessory_id')
            ->field('accessory_name as name,accessory_url as url')
            ->where('b.goods_id',$id)->select();
            $data['keywords'] = !empty($data['keywords']) ? explode(',',$data['keywords']) : [];
            
            //图片
            $images = $GoodsimgDb->alias('a')->join('sh_img i','i.img_id=a.img_id')->where('a.goods_id',$id)->field('img_url as url')->select();
            $data['goods_img'] = $images;

            //数值转化
            $data['is_units'] = $this->intFormatBool($data['is_units']);
            $data['is_hot'] = $this->intFormatBool($data['is_hot']);
            $data['is_new'] = $this->intFormatBool($data['is_new']);
            $data['is_use_initial_unit'] = $this->intFormatBool($data['is_use_initial_unit']);
            $data['is_use_extra1_unit'] = $this->intFormatBool($data['is_use_extra1_unit']);
            $data['is_use_extra2_unit'] = $this->intFormatBool($data['is_use_extra2_unit']);
            $data['is_client_price'] = $this->intFormatBool($data['is_client_price']);
            $data['extra1_unit_id'] = !empty($data['extra1_unit_id']) ? $data['extra1_unit_id'] : '';
            $data['extra2_unit_id'] = !empty($data['extra2_unit_id']) ? $data['extra2_unit_id'] : '';
            $data['extra1_unit_ratio'] = !empty($data['extra1_unit_ratio']) ? $data['extra1_unit_ratio'] : '';
            $data['extra2_unit_ratio'] = !empty($data['extra2_unit_ratio']) ? $data['extra2_unit_ratio'] : '';

            //获取翻译
            $data['initial_unit_name'] = $this->getUnitTrans($data['initial_unit_id']);
            $data['extra1_unit_name'] = $this->getUnitTrans($data['extra1_unit_id']);
            $data['extra2_unit_name'] = $this->getUnitTrans($data['extra2_unit_id']);

            foreach($data['level_price'] as &$v){
                $v['level_purchase'] = $this->intFormatBool($v['level_purchase']);
            }

            foreach($data['client_price'] as &$k){
                $k['client_purchase'] = $this->intFormatBool($k['client_purchase']);
            };
            //var_dump($data);die;
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    /**
     * 修改
     */
    public function update(Request $request)
    {

        $params = $request->param();
        //var_dump($params);die;
        if(!empty($params['goods_accessory'])){
            $goods_accessory = $params['goods_accessory'];
            unset($params['goods_accessory']);
        }
        
       if(!empty($params['goods_img'])){
        $goods_imgs = $params['goods_img'];
        unset($params['goods_img']);
       }

        try {
            $GoodsModel = new GoodsModel();
            $GoodsModel->startTrans();
            $imgModel = new Img();
            $imgDb = $imgModel->ImgDb();

            $accessoryModel = new accessoryModel();
            $AccessoryDb = $accessoryModel->AccessoryDb();
            $GoodsDb = $GoodsModel->GoodsDb();
            $id =  $params['goods_id'];
            $specification_id =  $params['specification_id'];
            $addGoods['update_goods_time'] = strtotime(date('Y-m-d H:i:s'));
            $addGoods['goods_name'] = $params['goods_name'];
            $addGoods['cate_id'] = $params['cate_id'];
            $addGoods['is_units'] = $this->boolFormatInt($params['is_units']);
            $addGoods['is_client_price'] = $this->boolFormatInt($params['is_client_price']);
            $addGoods['sort_num'] = $params['sort_num'];
            $addGoods['keywords'] = !empty($params['keywords']) ? implode(',',$params['keywords']) : '';
            //$addGoods['produced'] = $params['produced'];
            //$addGoods['expiration_date'] = $params['expiration_date'];
            //$addGoods['produced_time'] = strtotime($params['produced_time']);
            $addGoods['is_new'] = $this->boolFormatInt($params['is_new']);
            $addGoods['is_hot'] = $this->boolFormatInt($params['is_hot']);
            $addGoods['goods_sn'] = $params['goods_sn'];
            $addGoods['goods_code1'] = $params['goods_code1'];
            $addGoods['goods_code2'] = $params['goods_code2'];
            $addGoods['goods_code3'] = $params['goods_code3'];
            $addGoods['cost_price'] = $params['cost_price'];
            $addGoods['goods_state'] = $params['goods_state'];

            if(!empty($goods_imgs)){
                $addGoods['goods_img'] = $goods_imgs[0];
            }

            $GoodsDb->where('goods_id',$id)->update($addGoods);


            //商品描述
            $GoodsdescribeModel = new Goodsdescribe();
            $GoodsdescribeDb = $GoodsdescribeModel->GoodsdescribeDb();
            $addDescribe['describe_content'] =  $params['goods_describe'];
            $GoodsdescribeDb->where('goods_id',$id)->update($addDescribe);


            //图片
            if(!empty($goods_imgs)){
                $GoodsimgModel = new GoodsImgModel();
                $GoodsimgDb = $GoodsimgModel->GoodsimgDb();
                $GoodsimgModel->where('goods_id',$id)->delete();
                foreach ($goods_imgs as $v){
                    $iid = $imgDb->insertGetId(['img_url'=>$v]);
                    $addGoodsimg['img_id'] = $iid;
                    $addGoodsimg['goods_id'] = $id;
                    $GoodsimgDb->insert($addGoodsimg);
                }
            }

            //附件
            if(!empty($goods_accessory)){
                $GoodsAccessoryModel = new GoodsAccessory();
                $GoodsAccessoryDb = $GoodsAccessoryModel->GoodsAccessoryDb();
                $GoodsAccessoryModel->where('goods_id',$id)->delete();
                foreach ($goods_accessory as $v1){
                    $arrAccessory = explode('|',$v1);
                    $aid = $AccessoryDb->insertGetId(['accessory_url'=>$arrAccessory[0],'accessory_name'=>$arrAccessory[1]]);
                    $addGoodsAccessory['accessory_id'] = $aid;
                    $addGoodsAccessory['goods_id'] = $id;
                    $GoodsAccessoryDb->insert($addGoodsAccessory);
                }
            }

            //保存商品多单位
            $addUnit['initial_unit_id'] = $params['initial_unit_id'];
            $addUnit['is_use_initial_unit'] = $this->boolFormatInt($params['is_use_initial_unit']);
            $addUnit['initial_unit_price'] = $params['initial_unit_price'];
            $addUnit['extra1_unit_id'] = $params['extra1_unit_id'];
            $addUnit['extra1_unit_price'] = $params['extra1_unit_price'];
            $addUnit['extra1_unit_ratio'] = $params['extra1_unit_ratio'];
            $addUnit['is_use_extra1_unit'] = $this->boolFormatInt($params['is_use_extra1_unit']);
            $addUnit['extra2_unit_id'] = $params['extra2_unit_id'];
            $addUnit['extra2_unit_price'] = $params['extra2_unit_price'];
            $addUnit['extra2_unit_ratio'] = $params['extra2_unit_ratio'];
            $addUnit['is_use_extra2_unit'] = $this->boolFormatInt($params['is_use_extra2_unit']);
            $GoodsUnitsModel =  new GoodsUnits();
            $GoodsUnitsDb = $GoodsUnitsModel->GoodsUnitsDb();
            $GoodsUnitsDb->where('goods_id',$id)->update($addUnit);
            //保存商品客户价
           if(isset($params['client_price'])) {
               $client_price = $params['client_price'];
               $GoodsClientPrice = new GoodsClientPrice();
               $GoodsClientPriceDb = $GoodsClientPrice->GoodsClientPriceDb();
               $GoodsClientPriceDb->where('goods_id', $id)->delete();
               foreach ($client_price as $ClientPriceK => $ClientPriceV) {
                   $addClientPrice['client_id'] = $ClientPriceV['client_id'];
                   $addClientPrice['client_purchase'] = $this->boolFormatInt($ClientPriceV['client_purchase']);
                   $addClientPrice['client_price1'] = $ClientPriceV['client_price1'];
                   $addClientPrice['client_price2'] = $ClientPriceV['client_price2'];
                   $addClientPrice['client_price3'] = $ClientPriceV['client_price3'];
                   $addClientPrice['client_num'] = $ClientPriceV['client_num'];
                   $addClientPrice['goods_id'] = $id;
                   $GoodsClientPriceDb->insert($addClientPrice);
               }
           }
            //保存商品等级价
            $level_price = $params['level_price'];
          
            $GoodsLevelPrice = new GoodsLevelPrice();
            $GoodsLevelPriceDb = $GoodsLevelPrice->GoodsLevelPriceDb();
            $GoodsLevelPriceDb->where('goods_id',$id)->delete();
            foreach ($level_price as $levelPriceK =>$levelPriceV){
                $addLevelPrice['level_id'] = $levelPriceV['level_id'];
                $addLevelPrice['discount'] = $levelPriceV['discount'];
                $addLevelPrice['level_purchase'] = $this->boolFormatInt($levelPriceV['level_purchase']);
                $addLevelPrice['level_price1'] = ($levelPriceV['discount'] / 100) * $params['initial_unit_price'];
                $addLevelPrice['level_price2'] = !empty($params['extra1_unit_id']) ? ($levelPriceV['discount'] / 100) * $params['extra1_unit_price']  : 0;
                $addLevelPrice['level_price3'] =!empty($params['extra2_unit_id']) ? ($levelPriceV['discount'] / 100) * $params['extra2_unit_price'] : 0;
                $addLevelPrice['level_num'] = $levelPriceV['level_num'];
                $addLevelPrice['goods_id'] = $id;
                $GoodsLevelPriceDb->insert($addLevelPrice);
            }


            $GoodsModel->commit();
        } catch (Exception $e) {

            $GoodsModel->rollback();
            $this->serverErrorTerminate($e->getMessage());
        }
        $this->successTerminate();
    }
    /**
     * 删除
     */
    public function del(Request $request){
        $id = $request->get('goods_id');
        $goodsModel = new \app\shop\model\Goods();
        $goodsModel->where('goods_id',$id)->update(['is_delete'=>1]);

    }
    //展示页数据
    public function show(Request $request){
        $specification_id= $request->get('specification_id');
        $goods_id= $request->get('goods_id');
        $goodsModel = new \app\shop\model\Goods();
        $GoodsDb = $goodsModel->GoodsDb();
        $GoodsLevelPrice = new GoodsLevelPrice();
        $GoodsLevelPriceDb = $GoodsLevelPrice->GoodsLevelPriceDb();
        $GoodsClientPrice = new GoodsClientPrice();
        $GoodsClientPriceDb = $GoodsClientPrice->GoodsClientPriceDb();
        $GoodsImg = new GoodsImg();
        $GoodsimgDb = $GoodsImg->GoodsimgDb();
        $GoodsAccessory = new  GoodsAccessory();
        $GoodsAccessoryDb = $GoodsAccessory->GoodsAccessoryDb();
        $specificationName = [];
        $specificationValue = [[],[],[]];
        $specificationThis = ['','',''];
        try {
            $specificationGoods = $GoodsDb->where('specification_id',$specification_id)->select();
            if(count($specificationGoods)>0){

                $specificationName=[$specificationGoods[0]['specification1_name'],$specificationGoods[0]['specification2_name'],$specificationGoods[0]['specification3_name']];
            }
            $rGoodsData = [];
            foreach ($specificationGoods as $k=>$v){
                $tK = '';
                if($v['goods_id']==$goods_id){
                    $specificationThis[0] =  $v['specification1_value'];
                    $specificationThis[1] =  $v['specification2_value'];
                    $specificationThis[2] =  $v['specification3_value'];
                }
                $tK = $v['specification1_value'].'|~|'.$v['specification2_value'].'|~|'.$v['specification3_value'];
                if(!in_array($v['specification1_value'],$specificationValue[0]))$specificationValue[0][] = $v['specification1_value'];
                if(!in_array($v['specification2_value'],$specificationValue[1]))$specificationValue[1][] = $v['specification2_value'];
                if(!in_array($v['specification3_value'],$specificationValue[2]))$specificationValue[2][] = $v['specification3_value'];
                $rGoodsData[$tK] = $v;
                $rGoodsData[$tK]['produced_time'] = !empty($v['produced_time'])?'':date('Y-m-d',strtotime($v['produced_time']));
                $rGoodsData[$tK]['clientLevel'] = $GoodsLevelPriceDb->where('goods_id',$v['goods_id'])->select();
                $rGoodsData[$tK]['clientPrice'] = $GoodsClientPriceDb->where('goods_id',$v['goods_id'])->select();
                $rGoodsData[$tK]['goods_imgs'] = $GoodsimgDb->alias('a')->join('sh_img i','i.img_id=a.img_id')->where('a.goods_id',$v['goods_id'])->select();
                $rGoodsData[$tK]['goods_accessory'] = $GoodsAccessoryDb->alias('b')->join('sh_accessory c','b.accessory_id=c.accessory_id')->where('b.goods_id',$v['goods_id'])->select();
            }
            $redData = [
                'specificationThis'=>$specificationThis,
                'specificationName'=>$specificationName,
                'specificationValue'=>$specificationValue,
                'goodsData'=>$rGoodsData
            ];

            $this->successTerminate($redData);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }
    //通过客户id 获取商品价格
    //client_id:客户id
    //goods_id :商品id
    //
    public function clientGoodsPrice($client_id,$goods_id){
       //获取客户等级
       $ClientModel = new \app\shop\model\Client();
       //        $ClientModel->alias('')->join('sh_client_level')
//        $ClientLevelModel->where()
    }

    //布尔值转化数字
    public function boolFormatInt($bool){
        return $bool == 'true' ? 1 : 0;
    }

    //数字转化布尔值
    public function intFormatBool($int){
        return $int ? true : false;
    }

    //翻译单位
    public function getUnitTrans($unit){
        if(empty($unit)) return '';
        $unitModel = new Unit();
        return $unitModel->where('unit_id',$unit)->value('unit_name');
    }

    //获取前台商品列表
    public function productList(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->goodsList($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function allGoodsList(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->allGoodsList($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function show_goods(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->show_goods($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function appGoodsData(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->goodsList($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    public function adminGoodsList(Request $request){
        $GoodsModel = new GoodsModel();

        try {
            $params = $request->get();
            $data = $GoodsModel->adminGoods($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //展示页数据
    public function GoodsInfo(Request $request){
        $GoodsModel = new GoodsModel();
        try {
            $params = $request->get();
            $data = $GoodsModel->info($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //批量上下架
    public function batchSale(Request $request){
        $GoodsModel = new GoodsModel();
        $GoodsDb = $GoodsModel->GoodsDb();
        try {
            $params = $request->post();
            $GoodsDb->where('goods_id','in',$params['goods_id'])->update(['goods_state'=>$params['state']]);
            $this->successTerminate();
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

    //选择关联产品
    public function relation_goods(Request $request){
        $GoodsModel = new GoodsModel();
        try {
            $params = $request->get();
            $data = $GoodsModel->relation_goods($params);
            $this->successTerminate($data);
        } catch (Exception $e) {
            $this->serverErrorTerminate($e->getMessage());
        }
    }

}